package com.zhs.gccs.dao;

import com.zhs.gccs.model.GccsTaskData;
import com.zhs.gccs.model.GccsTaskFile;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.List;

//任务轨迹
public class TaskGccsDataDao {
    private final DbManager dbManager;

    public TaskGccsDataDao() {
        this.dbManager = XUtilsManager.getInstance().getDbManager();
    }

    public void addOrUpdate(GccsTaskData data) {
        try {
            dbManager.saveOrUpdate(data);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public List<GccsTaskData> findByPid(String pid) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("pid", "=", pid);
            List<GccsTaskData> first = dbManager
                    .selector(GccsTaskData.class).where(whereBuilder).findAll();
            return first;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public GccsTaskData findById(String id) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("id", "=", id);
            GccsTaskData first = dbManager
                    .selector(GccsTaskData.class).where(whereBuilder).findFirst();
            return first;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleteById(String dataId) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("id", "=", dataId);
            dbManager.delete(GccsTaskData.class,whereBuilder);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void deleteByPid(String pid) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("pid", "=", pid);
            dbManager.delete(GccsTaskData.class,whereBuilder);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    public void clearDB(){
        List<GccsTaskData> list = null;
        try {
            list = dbManager.selector(GccsTaskData.class).findAll();
            if(null!=list && list.size()>0){
                dbManager.delete(list);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public List<GccsTaskData> findListByTaskId(String taskId) {
        List<GccsTaskData> list = null;
        try {
            WhereBuilder whereBuilder = WhereBuilder
                    .b("taskId", "=", taskId);
            list = dbManager.selector(GccsTaskData.class).where(whereBuilder).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;
    }



}
